package com.mark.springsecurity.service.impl;

import com.mark.springsecurity.domain.SysAuthority;
import com.mark.springsecurity.exception.SysAuthorityNotFoundException;
import com.mark.springsecurity.repository.SysAuthorityRepository;
import com.mark.springsecurity.service.AuthorityService;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.Optional;

@Service
@Transactional
public class AuthorityServiceImpl implements AuthorityService {

    private final SysAuthorityRepository authorityRepository;

    public AuthorityServiceImpl(SysAuthorityRepository authorityRepository) {
        this.authorityRepository = authorityRepository;
    }

    @Override
    public void save(SysAuthority sysAuthority) {
        authorityRepository.save(sysAuthority);
    }

    @Override
    public SysAuthority getOne(Long id) {
        Optional<SysAuthority> optionalSysAuthority = authorityRepository.findById(id);
        if (optionalSysAuthority.isPresent()) return optionalSysAuthority.get();
        else throw new SysAuthorityNotFoundException("权限不存在");
    }
}
